<%--
  Created by IntelliJ IDEA.
  User: Douliusuo
  Date: 2022/4/30
  Time: 20:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>添加员工信息</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css" media="all">
        <link rel="stylesheet" href="${pageContext.request.contextPath}/css/public.css" media="all">
        <script src="${pageContext.request.contextPath}/js/jquery-3.6.0.min.js"></script>
        <style>
            body {
                background-color: #ffffff;
            }
        </style>
    </head>
    <body>
        <div class="layui-form layuimini-form">
            <div class="layui-form-item">
                <label class="layui-form-label required">员工编号</label>
                <div class="layui-input-block">
                    <input type="text" name="empno" lay-verify="required"  placeholder="请输入员工编号"
                           value="" class="layui-input">
                    <tip>填写自己管理的员工编号</tip>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">员工姓名</label>
                <div class="layui-input-block">
                    <input type="text" name="ename" lay-verify="required"  placeholder="请输入员工姓名"
                           value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">员工职务</label>
                <div class="layui-input-block">
                    <input type="text" name="job" lay-verify="required"  placeholder="请输入员工职务"
                           value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">上司编号</label>
                <div class="layui-input-block">
                    <input type="text" name="mgr" placeholder="请输入上司编号" value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">员工薪资</label>
                <div class="layui-input-block">
                    <input type="text" name="sal" placeholder="请输入员工薪资" value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">入职时间</label>
                <div class="layui-input-block">
                    <input type="date" name="hiredate" placeholder="请输入员工入职时间" value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">年终奖金</label>
                <div class="layui-input-block">
                    <input type="text" name="comm" placeholder="请输入员工年终奖金" value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">部门编号</label>
                <div class="layui-input-block">
                    <input type="text" name="deptno" placeholder="请输入员工所在部门编号" value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">备注信息</label>
                <div class="layui-input-block">
                    <textarea name="remark" class="layui-textarea" placeholder="请输入备注信息"></textarea>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认提交</button>
                    <%-- lay-submit lay-filter="backBtn" 去除，防止做 必填字段提示!!--%>
                    <button id="backmain" class="layui-btn layui-btn-normal">返回主页面</button>
                </div>
            </div>
        </div>
        <script src="${pageContext.request.contextPath}/layui/layui.js" charset="utf-8"></script>
      <script>
         $("#backmain").click(function () {
           alert("你还未填写或提交，确定要返回主页面吗？");
           window.location = "${pageContext.request.contextPath}/user/main";
         });
      </script>
        <script>
            layui.use(['form'], function () {
                var form = layui.form,
                    layer = layui.layer,
                    $ = layui.$;
                //监听提交
                // data.field 这个是提交的数据！！ 非常重要
                form.on('submit(saveBtn)', function (data) {
                    data = data.field;
                    // 阻止原有表单自动提交，我们使用ajax ，手动提交
                    // 要用jquey 先导入jquery
                    $.ajax({
                        // 请求数据的路径
                        url: '${pageContext.request.contextPath}/emp/addEmp',
                        // 数据格式封装
                        dataType: 'text',
                        data: {
                            'empno': data.empno,
                            'ename': data.ename,
                            'job': data.job,
                            'mgr': data.mgr,
                            'sal': data.sal,
                            'hiredate': data.hiredate,
                            'comm': data.comm,
                            'deptno': data.deptno
                        },
                        // result 会拿到 数据值 data, 参数名随意
                        success: function (result) {
                            alert("添加成功!")
                            // ‘1’ 和 “1” 有区别?
                            if (result == "1") {
                                // 成功返回主页面
                                window.location = "${pageContext.request.contextPath}/user/main";
                            } else {
                                // 失败返回错误信息
                                layer.msg('添加失败！', {icon: 5});
                            }
                        },
                        error: function (re) {
                            alert("失败!!")
                            alert(re.toString())
                        }
                    })
                    return false;
                });

              // 监听提交2,返回主页面,同样可以实现
              /*
              改写成这个就可以了
              <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认提交</button>
                    <button id="backmain" class="layui-btn layui-btn-normal" lay-submit lay-filter="backBtn">返回主页面</button>
                </div>
               */
                <%--form.on('submit(backBtn)', function () {--%>
                <%--    alert("确定返回主页面?");--%>
                <%--    window.location = "${pageContext.request.contextPath}/user/main";--%>
                <%--    return false;--%>
                <%--});--%>
            });
        </script>
    </body>
</html>
